package edu.sfsu.powerrangers.jeopardy.editor;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import edu.sfsu.powerrangers.jeopardy.gamestate.Game;
import edu.sfsu.powerrangers.jeopardy.gamestate.Round;

public class GamePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTextField nameField;
	private JTextField descriptionField;
	private JTabbedPane roundPane;
	
	public GamePanel(int rounds, int rows, int cols, boolean finalRound) {
		super(new BorderLayout(4,4));
		JLabel nameLabel = new JLabel("Name:");
		nameLabel.setHorizontalAlignment(JLabel.TRAILING);
		JLabel descriptionLabel = new JLabel("Description:");
		descriptionLabel.setHorizontalAlignment(JLabel.TRAILING);
		JPanel labelPanel = new JPanel(new GridLayout(0,1,4,4));
		labelPanel.add(nameLabel);
		labelPanel.add(descriptionLabel);
		nameField = new JTextField();
		descriptionField = new JTextField();
		JPanel fieldPanel = new JPanel(new GridLayout(0,1,4,4));
		fieldPanel.add(nameField);
		fieldPanel.add(descriptionField);
		JPanel headerPanel = new JPanel(new BorderLayout(4,4));
		headerPanel.add(labelPanel, BorderLayout.LINE_START);
		headerPanel.add(fieldPanel, BorderLayout.CENTER);
		roundPane = new JTabbedPane();
		while (rounds-->0) {
			roundPane.add(new RoundPanel(rows, cols), "Round " + (roundPane.getComponentCount()+1));
		}
		if (finalRound) {
			roundPane.add(new FinalRoundPanel(), "Final Round");
		}
		
		JButton addRndBtn = new JButton("Add Round");
		addRndBtn.putClientProperty("JComponent.sizeVariant", "mini");
		addRndBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setRoundCount(getRoundCount()+1);
			}
		});
		JButton remRndBtn = new JButton("Remove Round");
		remRndBtn.putClientProperty("JComponent.sizeVariant", "mini");
		remRndBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (getRoundCount() > 1) {
					setRoundCount(getRoundCount()-1);
				}
			}
		});
		JButton addFinBtn = new JButton("Add Final Round");
		addFinBtn.putClientProperty("JComponent.sizeVariant", "mini");
		addFinBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setFinalRoundCount(getFinalRoundCount()+1);
			}
		});
		JButton remFinBtn = new JButton("Remove Final Round");
		remFinBtn.putClientProperty("JComponent.sizeVariant", "mini");
		remFinBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (getFinalRoundCount() > 0) {
					setFinalRoundCount(getFinalRoundCount()-1);
				}
			}
		});
		JPanel buttonPanel1 = new JPanel(new GridLayout(1,0,4,4));
		buttonPanel1.add(addRndBtn);
		buttonPanel1.add(remRndBtn);
		buttonPanel1.add(addFinBtn);
		buttonPanel1.add(remFinBtn);
		buttonPanel1.setMaximumSize(buttonPanel1.getPreferredSize());
		JPanel buttonPanel2 = new JPanel();
		buttonPanel2.setLayout(new BoxLayout(buttonPanel2, BoxLayout.X_AXIS));
		buttonPanel2.add(Box.createHorizontalGlue());
		buttonPanel2.add(buttonPanel1);
		buttonPanel2.add(Box.createHorizontalGlue());
		
		add(headerPanel, BorderLayout.PAGE_START);
		add(roundPane, BorderLayout.CENTER);
		add(buttonPanel2, BorderLayout.PAGE_END);
	}
	
	public int getFinalRoundCount() {
		int count = 0;
		for (Component c : roundPane.getComponents()) {
			if (c instanceof FinalRoundPanel) {
				count++;
			}
		}
		return count;
	}
	
	public void setFinalRoundCount(int count) {
		if (count > 0) {
			for (Component c : roundPane.getComponents()) {
				if (c instanceof FinalRoundPanel) {
					return;
				}
			}
			roundPane.add(new FinalRoundPanel(), "Final Round");
			roundPane.validate();
		} else {
			for (Component c : roundPane.getComponents()) {
				if (c instanceof FinalRoundPanel) {
					roundPane.remove(c);
				}
			}
			roundPane.validate();
		}
	}
	
	public int getRoundCount() {
		int count = 0;
		for (Component c : roundPane.getComponents()) {
			if (c instanceof RoundPanel) {
				count++;
			}
		}
		return count;
	}
	
	public void setRoundCount(int count) {
		for (Component c : roundPane.getComponents()) {
			if (c instanceof RoundPanel) {
				if (count > 0) {
					count--;
				} else {
					roundPane.remove(c);
				}
			}
		}
		while (count-->0) {
			roundPane.add(new RoundPanel(5,6), "Round " + (getRoundCount()+1), getRoundCount());
		}
	}
	
	public Game getGame() {
		Game g = new Game(nameField.getText(), descriptionField.getText());
		for (Component c : roundPane.getComponents()) {
			if (c instanceof FinalRoundPanel) {
				g.setFinalRound(((FinalRoundPanel)c).getFinalRound());
			} else {
				g.add(((RoundPanel)c).getRound());
			}
		}
		return g;
	}
	
	public void setGame(Game g) {
		nameField.setText(g.getName());
		descriptionField.setText(g.getDescription());
		roundPane.removeAll();
		for (Round r : g) {
			RoundPanel rp = new RoundPanel(0,0);
			rp.setRound(r);
			roundPane.add(rp, "Round " + (roundPane.getComponentCount()+1));
		}
		if (g.getFinalRound() != null) {
			FinalRoundPanel frp = new FinalRoundPanel();
			frp.setFinalRound(g.getFinalRound());
			roundPane.add(frp, "Final Round");
		}
		roundPane.validate();
	}
}
